package com.ylbh.modules.shoppingcoupon.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.pojo.goods.dto.GoodsClassDTO;
import com.ylbh.common.pojo.shoppingcoupon.dto.QueryCouponGoodsDTO;
import com.ylbh.common.pojo.shoppingcoupon.dto.ShoppingCouponDTO;
import com.ylbh.common.pojo.shoppingcoupon.vo.AddCouponGoodsListVO;
import com.ylbh.common.pojo.shoppingcoupon.vo.ShoppingCouponListVO;
import com.ylbh.common.utils.IdWorker;
import com.ylbh.common.utils.ShiroUtils;
import com.ylbh.modules.activity.mapper.ActivityGoodsMapper;
import com.ylbh.modules.banner.mapper.AppHomeBannerMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.utils.PageUtils;

import com.ylbh.modules.shoppingcoupon.mapper.ShoppingCouponMapper;
import com.ylbh.modules.shoppingcoupon.entity.ShoppingCoupon;
import com.ylbh.modules.shoppingcoupon.service.ShoppingCouponService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service("shoppingCouponService")
public class ShoppingCouponServiceImpl extends ServiceImpl<ShoppingCouponMapper, ShoppingCoupon> implements ShoppingCouponService {

    @Autowired
    private ShoppingCouponMapper shoppingCouponMapper;

    @Autowired
    private ActivityGoodsMapper activityGoodsMapper;

    @Autowired
    private AppHomeBannerMapper appHomeBannerMapper;

    /**
     * 商城优惠券列表信息
     * @param shoppingCouponDTO
     * @return
     */
    @Override
    public PageUtils queryCouponList(ShoppingCouponDTO shoppingCouponDTO) {
        Page<Integer> page = new Page<>(shoppingCouponDTO.getPage(), shoppingCouponDTO.getLimit());
        List<ShoppingCouponListVO> list = shoppingCouponMapper.selectCouponList(shoppingCouponDTO, page);
        return new PageUtils(new Page<ShoppingCouponListVO>().setRecords(list).setTotal(page.getTotal()));
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<GoodsClassDTO> queryClass(Integer type) {
        List<GoodsClassDTO> list = null;
        if (type == 2) {
            list = activityGoodsMapper.selectClassList();
        } else if (type == 3) {
            list = appHomeBannerMapper.selectSpecial();
        }
        return list;
    }

    /**
     * 根据类型和id查询商品
     * @param type
     * @param id
     * @return
     */
    @Override
    public PageUtils queryGoodsByTypeOrId(QueryCouponGoodsDTO queryCouponGoodsDTO) {
        List<AddCouponGoodsListVO> list = null;
        Page<Integer> page = new Page<>(queryCouponGoodsDTO.getPage(), queryCouponGoodsDTO.getLimit());
        if (queryCouponGoodsDTO.getScopeOfApplication() == 2) {
            // 商品的一级分类id去查询商品
            list = shoppingCouponMapper.selectGoodsByGoodsClass(queryCouponGoodsDTO, page);
        } else if (queryCouponGoodsDTO.getScopeOfApplication() == 3) {
            // 根据id去专题中查询商品
            list = shoppingCouponMapper.selectGoodsBySpecialId(queryCouponGoodsDTO, page);
        } else if (queryCouponGoodsDTO.getScopeOfApplication() == 4) {
            // 查询商品所有商品
            list = shoppingCouponMapper.selectAllGoods(queryCouponGoodsDTO, page);
        }
        return new PageUtils(new Page<AddCouponGoodsListVO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 新增商品优惠券
     * @param shoppingCoupon
     */
    @Override
    public void addCoupon(ShoppingCoupon shoppingCoupon) {
        shoppingCoupon.setId(new IdWorker().nextId());
        shoppingCoupon.setState(2);
        shoppingCoupon.setCreateTime(new Date());
        shoppingCoupon.setCreateUserId(ShiroUtils.getUserId().toString());
        shoppingCoupon.setAlreadyLedNumber(0);
        shoppingCoupon.setStock(shoppingCoupon.getCirculation());
        if (shoppingCoupon.getReleaseType() == 1) {
            shoppingCoupon.setWayOfReceiving(1);
            shoppingCoupon.setCouponStandard("0");
        } else {
            shoppingCoupon.setWayOfReceiving(2);
        }
        if (null == shoppingCoupon.getCirculation()) {
            shoppingCoupon.setCirculation(0);
        }
        if (shoppingCoupon.getTermType() == 1) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String startTime = null;
            String endTime = null;
            try {
                endTime = sdf.format(sdf.parse(shoppingCoupon.getEndTime()).getTime());
                startTime = sdf.format(sdf.parse(shoppingCoupon.getStartTime()).getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            shoppingCoupon.setEndTime(endTime + " 23:59:59");
            shoppingCoupon.setStartTime(startTime + " 00:00:00");
        } else {
            shoppingCoupon.setStartTime(null);
            shoppingCoupon.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
        shoppingCouponMapper.insertShoppingCoupon(shoppingCoupon);
    }

    /**
     * 查询商城优惠券详情
     * @param id
     * @return
     */
    @Override
    public ShoppingCoupon getCouponInfoById(String id) {
        return shoppingCouponMapper.selectCouponInfoById(id);
    }

    /**
     * 修改商城优惠券信息
     * @param shoppingCoupon
     */
    @Override
    public void modifyCouponById(ShoppingCoupon shoppingCoupon) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = null;
        String endTime = null;
        try {
            startTime = sdf.format(sdf.parse(shoppingCoupon.getStartTime()).getTime());
            endTime = sdf.format(sdf.parse(shoppingCoupon.getEndTime()).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        shoppingCoupon.setStartTime(startTime + " 00:00:00");
        shoppingCoupon.setEndTime(endTime + " 23:59:59");
        shoppingCoupon.setUpdateTime(new Date());
        shoppingCoupon.setUpdateUserId(ShiroUtils.getUserId().toString());
        shoppingCouponMapper.updateCouponById(shoppingCoupon);
    }

    /**
     * 商城优惠券启用与关闭
     * @param id
     * @param state
     */
    @Override
    public void modifyCouponStateById(String id, Integer state) {
        shoppingCouponMapper.updateCouponStateById(id, state);
    }

    /**
     * 删除商城优惠券
     * @param ids
     */
    @Override
    public void removeCouponByIds(List<String> ids) {
        shoppingCouponMapper.updateCouponByIds(ids);
    }
}
